using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using System.Drawing;
using Utilities.Security; 

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        removeSession();
        username_id.Focus();
        if (!IsPostBack)
        {   
            lbwarning.Visible = false;
            //this.InitData();
        }
    }

    //private void InitData()
    //{
    //    int nCurrentLanguage = Support.GetCurrentLanguage();

    //    Company objCompany = new Company();
    //    DataTable dtbCompany = new DataTable();
    //    dtbCompany = objCompany.Search();

    //    switch (nCurrentLanguage)
    //    { 
    //        case 1:
    //            Page.Title = ConvertData.ConvertToString(dtbCompany.Rows[0]["CompanyName1"]);
    //            lblCompanyName.Text = ConvertData.ConvertToString(dtbCompany.Rows[0]["CompanyName1"]);
    //            break;
    //        case 2:
    //            Page.Title = ConvertData.ConvertToString(dtbCompany.Rows[0]["CompanyName2"]);
    //            lblCompanyName.Text = ConvertData.ConvertToString(dtbCompany.Rows[0]["CompanyName2"]);
    //            break;
    //        case 3:
    //            Page.Title = ConvertData.ConvertToString(dtbCompany.Rows[0]["CompanyName3"]);
    //            lblCompanyName.Text = ConvertData.ConvertToString(dtbCompany.Rows[0]["CompanyName3"]);
    //            break;

    //    }
    //}
    protected void but_login_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        nDefaultLanguage = nDefaultLanguage - 1;
        bool isCheckUser = false;
        isCheckUser = isUser();
        if (isCheckUser)
        {
            string sType = ConvertData.ConvertToString(Session[Config.SESSION_USER_TYPE]);
            if (sType != "3")
            { Response.Redirect(Pages.BackEnds.ADMIN); }
            else
            { Response.Redirect(Pages.FrontEnds.HOME); }

        }

        else
        {
            lbwarning.Text = MessagesAlert.LOGIN_FAIL[nDefaultLanguage];
            lbwarning.Visible = true;
            username_id.Focus();
        }
    }

    private bool isUser()
    {
        bool isUser = false;
        string sEmail = username_id.Text.Trim();
        string sUsername = "";
        sEmail = sEmail.ToLower();
        string sPassword = password.Text.Trim();
        Users objUser = new Users();
        DataTable dtbUser = new DataTable();

        try
        {
            dtbUser = objUser.GetUserByEmail(sEmail, EnumeType.ACTIVE);
            string sCurrentEmail = ConvertData.ConvertToString(dtbUser.Rows[0]["Email"]);
            string sCurrentPassword = ConvertData.ConvertToString(dtbUser.Rows[0]["Password"]);
            sUsername = ConvertData.ConvertToString(dtbUser.Rows[0]["Username"]);
            string sUserType = ConvertData.ConvertToString(dtbUser.Rows[0]["RoleID"]);
            ClsEncrypt obj = new ClsEncrypt();
            string sDeCodePass = obj.Decrypt(sCurrentPassword);
            if (sEmail == sCurrentEmail && sPassword == sDeCodePass)
            {
                Session[Config.SESSION_USERNAME] = ConvertData.ConvertToString(sUsername);
                Session[Config.SESSION_USEREMAIL] = ConvertData.ConvertToString(sEmail);
                Session[Config.SESSION_USER_TYPE] = ConvertData.ConvertToString(sUserType);

                isUser = true;
            }
            else
                isUser = false;
        }
        catch (Exception)
        {
            isUser = false;
        }
        return isUser;
    }

    private void removeSession()
    {
        Session[Config.SESSION_USERNAME] = null;
        Session[Config.SESSION_USEREMAIL] =null;
        Session[Config.SESSION_USER_TYPE] = null;
    }
}
